<?php
// inc/class/config.php
namespace smp;

use smp\Base;
use smp\Database;
use smp\Database\libSQL as SQL;

class Config extends Base {

	// ClassProperties ->
	protected static $backup = array();
	protected static $changed = false;
	protected static $errno, $error, $trace, $errorpage;
	protected static $readonly = array('error', 'errno', 'trace', 'readonly', 'secret', 'version');
	protected static $secret = array('readonly', 'secret');

	// ConfigProperties -> -!
	protected static $version = version;
	protected static $debug = false;
	protected static $savelog = false;
	protected static $lockdown = false;
	protected static $pages = array();
	protected static $title = 'Source MotD Plus';
	protected static $email = 'source-motd-plus@googlegroups.com';
	protected static $website = 'http://code.google.com/p/source-motd-plus/';

	// getVersion ->
	public static function getVersion() { return static::$version; }

	// getSavelog ->
	public static function getSavelog() { return static::$savelog; }

	// getDebug ->
	public static function getDebug() { return static::$debug; }

	// load ->
	public static function load() {
		static::setTrace('load');
		if(!$db = Database::getInstance()) { // -!
			static::$errno = Database::errno();
			static::$error = Database::error();
			static::$errorpage = Database::errorpage();
		return false;
		}
		if(!$sql = $db->query(SQL::getQuery('Config.Load'))) { // -!
			static::$errno = 1;
			static::$error = 'Unable to read config from database!';
			static::$errorpage = 'err.config.load.htm';
		return false;
		}
		$res = $sql->fetch_assoc();
		$sql->close();
		if(!$sql = $db->query(SQL::getQuery('Config.ReadPages'))) { // -!
			static::$errno = 1;
			static::$error = 'Unable to read pages from database!';
			static::$errorpage = 'err.config.load.htm';
		return false;
		}
		while($r = $sql->fetch_assoc()) {
			$res['pages'][$r['alias']] = $r['path'];
		}
		$sql->close();
		static::$backup = $res;
		static::$version = $res['version'];
		static::$debug = (bool) $res['debug'];
		static::$lockdown = (bool) $res['lockdown'];
		static::$savelog = (bool) $res['savelog'];
		static::$pages = $res['pages'];
		static::$title = $res['title'];
		static::$website = $res['website'];
		static::$email = $res['email'];
		unset($sql, $res, $db);
	return true;
	}

	// save ->
	public static function save() {
		static::setTrace('save');
		if(!static::$changed) { // -!
			static::$errno = 1;
			static::$error = 'No properties have changed!';
			static::$errorpage = 'err.config.save.htm';
		return false;
		}
		if(!$db = Database::getInstance()) { // -!
			static::$errno = Database::errno();
			static::$error = Database::error();
			static::$errorpage = Database::errorpage();
		return false;
		}
		$stmt = $db->prepare(libSQL::getQuery('Config.Save'));
		if(!$stmt->bind_param(libSQL::getQuery('Config.Save.Param'),
													static::$debug,
													static::$lockdown,
													static::$title,
													static::$website,
													static::$email,
													static::$savelog,
													static::$version)) { // -!
			static::$errno = 1;
			static::$error = 'Unable to bind params!';
			static::$errorpage = 'err.config.save.htm';
		return false;
		}
		if(!$stmt->execute()) { // -!
			static::$errno = 1;
			static::$error = 'Unable to write config!';
			static::$errorpage = 'err.config.save.htm';
		return false;
		}
		$stmt->close();
		static::$changed = NULL;
		unset($stmt, $db);
	return true;
	}

	// update ->
	public static function update($config, $direct = false) {
		static::setTrace('update');
		if(!is_array($config)) { // -!
			static::$errno = 1;
			static::$error = 'Argument $config has to be an array("property" => "value")!';
			static::$errorpage = 'err.config.update.htm';
		return false;
		}
		foreach($config as $prop => $val) {
			if(!isset(static::$$prop)) { // -!
				static::$errno = 1;
				static::$error = 'Unkown property: '.$prop.'!';
				static::$errorpage = 'err.config.update.htm';
			return false;
			}
			if(static::isReadonly($prop)) { // -!
				static::$errno = 1;
				static::$error = $prop.' is readonly!';
				static::$errorpage = 'err.config.update.htm';
			return false;
			}
			static::$$prop = $val;
			unset($prop, $val);
		}
		static::$changed = true;
		if($direct and !static::save()) {
			return false;
		}
	return true;
	}

}

?>